package com.nebulas.nettyhex.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.timeout.IdleStateHandler;

public class RuoYiNettyChannelInitializer<SocketChannel> extends ChannelInitializer<Channel> {



    private static final int READ_IDEL_TIME_OUT = 660; // 读超时，单位是秒，为:11分钟检测一次是否超时
    private static final int WRITE_IDEL_TIME_OUT = 0;// 写超时
    private static final int ALL_IDEL_TIME_OUT = 0; // 所有超时


    @Override
    protected void initChannel(Channel ch) throws Exception {

//        //半包/拆包/粘包数据处理
//        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(1, 1);
//        buffer.writeByte('#');
//        //设置包的字节长度，数据超过2048个字节则服务端不会收到。
//        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(2048,buffer));

        ChannelPipeline pipeline = ch.pipeline();
        // 自定义的十六进制解码器
        pipeline.addLast(new HexDecoder());

        //设置超时时间，多久没有收到消息，则认为是断开连接状态
        pipeline.addLast(new IdleStateHandler(READ_IDEL_TIME_OUT,WRITE_IDEL_TIME_OUT,ALL_IDEL_TIME_OUT));
        /**
         * 自定义ChannelInboundHandlerAdapter
         */
        pipeline.addLast(new RuoYiNettyChannelInboundHandlerAdapter());
    }

}
